<?php
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/interfaces/IUserAccessDao.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/interfaces/IConnectionAccessDao.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/factory/ConnectionFactory.php';

	error_reporting (E_ALL ^ E_NOTICE);
	
	$debug = $_SESSION['debug'];

	class   MySqlUserAccessDao implements IUserAccessDao {
  
 	 	/**
		 * Constructor
		 */
	 public function __construct () {}
	 
  
     public function createUser($user) 
	 {
		 	
		$pkUsername = $user -> getPkUsername();
		$password = $user -> getPassword();
		$firstName = $user -> getFirstName();
		$secondName = $user -> getSecondName();
		$firstLastName = $user ->  getFirstLastName();
		$secondLastName = $user -> getSecondLastName();	
		$emailAddress = $user -> getEmailAddress();
		$fkIdRole = $user -> getFkIdRole();	

		 
		
		 
		$connection = ConnectionFactory::getMySqlConnectionAccessDao();
		$conn=$connection->openConnection();
		$query = "INSERT INTO USER 
		           VALUES ('$pkUsername','$password','$firstName','$secondName','$firstLastName','$secondLastName','$emailAddress',2,1)";
	 
	 
	 
	    $sentence=mysql_query($query,$conn);
	       if ($sentence){
			    return true;
			   }
		  return false;
	 
	 }
	
	 
	 public function validateUser($username, $password) {	   
		$connection = ConnectionFactory :: getMySqlConnectionAccessDao();
		$conn = $connection -> openConnection();
		
		$query = "SELECT d.name
				  FROM USER u, JOB_HISTORY j, DEPARTMENT d 
				  WHERE u.pk_username = '$username' 
				  AND u.password = '$password'
				  AND u.fk_id_number_employee = j.pk_fk_id_number_employee
				  AND j.pk_fk_id_department = d.pk_id";
		
		$GLOBALS['debug'] -> log($query, "Query");
		
		$sentence = mysql_query($query, $conn);
		
		$conn = $connection -> closeConnection($conn);
		
		return $sentence;
	 }
	 
	 
	  /**
		 * Selects all Users
		 */
		public function selectUsers() {
			$connection = ConnectionFactory :: getMySqlConnectionAccessDao();
			$conn = $connection -> openConnection();
			
			
					$query = "SELECT pk_username,first_name,second_name,first_last_name,
					          second_last_name,email_address
							  FROM USER";
				
			
			$sentence = mysql_query($query, $conn);
				
			$conn = $connection -> closeConnection($conn);
				$GLOBALS['debug']->log($query,'Funcion DAO');
			return $sentence;
		 }
		 
  
  /* send mail user
  */
	    public function sendEmail ($email) {
		 
		$connection = ConnectionFactory::getMySqlConnectionAccessDao();
		$conn=$connection->openConnection();
		
	  $GLOBALS['debug'] -> log($email,"Entro Dao  send email ");
		 $query = "SELECT pk_username,password,email_address
				   FROM USER
				   WHERE `email_address`='$email'";
	 
	    $sentence=mysql_query($query,$conn);
			$conn = $connection -> closeConnection($conn);
	       if ($sentence){
			    $GLOBALS['debug'] -> log("Salio Dao  send email ");		
			    return $sentence;
			   }
			   
	   
		  return false;
	 
	 }
  
  }


?>